from django.db import models
from apps.users.models import User

class Team(models.Model):
    CATEGORY_CHOICES = [
        ('study', '学习小组'),
        ('project', '项目小组'),
        ('interest', '兴趣小组'),
    ]
    
    LEVEL_CHOICES = [
        ('beginner', '初级'),
        ('intermediate', '中级'),
        ('advanced', '高级'),
    ]
    
    STATUS_CHOICES = [
        ('active', '进行中'),
        ('completed', '已结束'),
    ]

    name = models.CharField(max_length=100, verbose_name='小组名称')
    description = models.TextField(verbose_name='小组描述')
    goal = models.TextField(verbose_name='学习目标')
    creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_teams')
    start_date = models.DateField(verbose_name='开始日期')
    end_date = models.DateField(verbose_name='结束日期')
    max_members = models.IntegerField(default=5)
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='study')
    level = models.CharField(max_length=20, choices=LEVEL_CHOICES, default='beginner')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    # 新增字段 completed_tasks 用于记录已完成任务数量
    completed_tasks = models.IntegerField(default=0, verbose_name='已完成任务数')

    class Meta:
        db_table = 'team'
        ordering = ['-created_at']

    def __str__(self):
        return self.name

class TeamMember(models.Model):
    ROLE_CHOICES = (
        ('creator', '创建者'),
        ('admin', '管理员'),
        ('member', '成员'),
    )

    team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name='团队')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='member', verbose_name='角色')
    joined_at = models.DateTimeField(auto_now_add=True, verbose_name='加入时间')

    class Meta:
        db_table = 'team_member'
        verbose_name = '团队成员'
        verbose_name_plural = verbose_name
        unique_together = ('team', 'user')
        ordering = ['-joined_at']

    def __str__(self):
        return f'{self.team.name} - {self.user.username}'

class TeamSprint(models.Model):
    team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name='团队')
    target_hours = models.FloatField(default=20, verbose_name='目标学时')
    completed_hours = models.FloatField(default=0, verbose_name='已完成学时')
    status = models.CharField(
        max_length=20,
        choices=[
            ('ongoing', '进行中'),
            ('completed', '已完成')
        ],
        default='ongoing',
        verbose_name='状态'
    )
    start_time = models.DateTimeField(auto_now_add=True, verbose_name='开始时间')
    end_time = models.DateTimeField(null=True, blank=True, verbose_name='结束时间')

    class Meta:
        db_table = 'team_sprint'
        verbose_name = '团队冲刺'
        verbose_name_plural = verbose_name

class SprintRecord(models.Model):
    sprint = models.ForeignKey(TeamSprint, on_delete=models.CASCADE, verbose_name='冲刺')
    member = models.ForeignKey(TeamMember, on_delete=models.CASCADE, verbose_name='成员')
    study_hours = models.FloatField(default=0, verbose_name='学习时长')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='记录时间')

    class Meta:
        db_table = 'sprint_record'
        verbose_name = '冲刺记录'
        verbose_name_plural = verbose_name

class TeamTask(models.Model):
    team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name='团队')
    title = models.CharField(max_length=100, verbose_name='任务标题')
    description = models.TextField(blank=True, null=True, verbose_name='任务描述')
    status = models.CharField(
        max_length=20,
        choices=[
            ('pending', '待完成'),
            ('completed', '已完成')
        ],
        default='pending',
        verbose_name='状态'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    completed_at = models.DateTimeField(null=True, blank=True, verbose_name='完成时间')

    class Meta:
        db_table = 'team_task'
        verbose_name = '团队任务'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

class TeamRanking(models.Model):
    """团队排行榜"""
    team = models.ForeignKey('Team', on_delete=models.CASCADE, related_name='rankings', verbose_name='团队')
    score = models.IntegerField(default=0, verbose_name='得分')
    sprint_count = models.IntegerField(default=0, verbose_name='冲刺次数')
    total_hours = models.FloatField(default=0, verbose_name='总学习时长')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'team_ranking'
        verbose_name = '团队排行'
        verbose_name_plural = verbose_name
        ordering = ['-score']
